package org.example.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.mapper.EmpMapper;
import org.example.pojo.Emp;
import org.example.pojo.Query;
import org.example.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Override
    public Query list(Integer page, Integer pageSize, String keyword, Integer gender, LocalDate beginTime, LocalDate endTime) {
        // 设置分页参数
        PageHelper.startPage(page, pageSize);
        //  执行查询
        List<Emp> empList = empMapper.list(keyword, gender, beginTime, endTime);
        Page<Emp> p = (Page<Emp>) empList;
        // 返回Query对象
//        Integer limit = (page - 1) * pageSize;
//        List<Emp> empList = empMapper.list(limit, pageSize);
//        Long total = empMapper.total();
        return new Query(p.getTotal(), p.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.deleteEmpByIds(ids);
    }

    @Override
    public void insert(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(null);
        empMapper.insertEmp(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.getUserByUserNameAndPassword(emp);
    }
}
